package com.blt.car.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.blt.car.R;
import com.blt.car.base.BaseActivity;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.home.adapter.MessageAdapter;
import com.blt.car.journey.JourneyDetailActivity;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;

import java.util.List;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.message.MessageApi;
import blt.app_core.api.message.bean.MessageInfo;

/**
 * Created by xgp on 2017/6/18.
 */

public class MessageActivity extends BaseTitleBarActivity implements View.OnClickListener {

    private ListView mListView;
    private MessageAdapter messageAdapter;
    private int page;
    private int pageSize = 15;
    private LinearLayout bottomLayut;
    private boolean isEdit = true;
    private boolean isSelect = true;
    private PtrClassicFrameLayout frameLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected void initData() {
        super.initData();
        CoreManager.getCore(MessageApi.class).getMsgList(page, pageSize);
    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("消息");
        mTitleBar.getLeftImage().setImageResource(R.mipmap.back_icon);
        mTitleBar.setRightText("编辑");
        bottomLayut = holder.get(R.id.msg_bottom_layout);
        holder.get(R.id.msg_tv_delete);
        holder.setOnClickListener(this, R.id.msg_tv_delete, R.id.msg_tv_all_select);
        mTitleBar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit) {
                    mTitleBar.setRightText("取消");
                    bottomLayut.setVisibility(View.VISIBLE);
                    messageAdapter.edit(true);
                    isEdit = false;
                } else {
                    mTitleBar.setRightText("编辑");
                    bottomLayut.setVisibility(View.GONE);
                    messageAdapter.edit(false);
                    messageAdapter.selectAll(false);
                    isEdit = true;
                }
            }
        });
        mListView = holder.get(R.id.message_list);
        messageAdapter = new MessageAdapter(this);
        mListView.setAdapter(messageAdapter);

        frameLayout = holder.get(R.id.message_frame);
        frameLayout.setLoadMoreEnable(true);
        frameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                CoreManager.getCore(MessageApi.class).getMsgList(page, pageSize);
            }
        });
        frameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                page++;
                CoreManager.getCore(MessageApi.class).getMsgList(page, pageSize);
            }
        });


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int msgType = messageAdapter.getItem(position).msgType;
                switch (msgType) {
                    case 10:
//                        openActivity(CarDetailActivity.class);
                        break;
                    case 20:
//                        openActivity(JourneyDetailActivity.class);
                        break;
                    case 30:
                        break;
                    case 40:
                        break;
                    case 50:
                        break;
                }
            }
        });
    }


    @CoreEvent(coreClientClass = MessageApi.class)
    public void onMessage(List<MessageInfo> infos) {
        if (page == 1) {
            messageAdapter.clear();
            frameLayout.refreshComplete();
        } else {
            frameLayout.loadMoreComplete(true);
        }
        messageAdapter.addData(infos);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.msg_tv_delete:
                messageAdapter.deleteItem();
                break;
            case R.id.msg_tv_all_select:
                if (isSelect) {
                    messageAdapter.selectAll(true);
                    isSelect = false;
                } else {
                    messageAdapter.selectAll(false);
                    isSelect = true;
                }

//                CoreManager.getCore(MessageApi.class).lookMsg(13);
                break;
        }
    }
}
